export function generate16CharUUID(): string {
    // 生成随机字符
    const randomChar = (): string => {
        const random = Math.random() * 16 | 0;
        return random.toString(16);
    };

    // 生成UUID的各个部分
    let uuid = '';
    for (let i = 0; i < 16; i++) {
        uuid += randomChar();
    }

    return uuid;
}

// 生成yyyy-mm-dd
export function formatDateToYYYYMMDD(): string {
    const date = new Date();
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
}
// Define an interface for the network information
export interface NetworkInfo {
    type: string;
    rtt?: number;
    downlink?: number;
}

// Extend the Navigator interface to include the connection property
declare global {
    interface Navigator {
        connection?: {
            effectiveType: string;
            rtt: number;
            downlink: number;
            // You can add more properties if needed
        };
    }
}

// Function to get network information
export function getNetWorkInfo(): NetworkInfo {
    if (!navigator.onLine) {
        return {
            type: 'offline',
            rtt: 0,
            downlink: 0
        };
    }

    // Check if the Network Information API is available
    if (!navigator.connection) {
        return {
            type: 'unknown',
            rtt: undefined,
            downlink: undefined
        };
    }

    return {
        type: navigator.connection.effectiveType,
        rtt: navigator.connection.rtt,
        downlink: navigator.connection.downlink
    };
}

// 深拷贝
export function deepClone<T>(source: T): T {
    if (!source && typeof source !== 'object') {
        throw new Error('Invalid arguments for deepClone');
    }

    const targetObj: any = Array.isArray(source) ? [] : {};

    for (const key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
            const value = source[key];

            if (value && typeof value === 'object') {
                targetObj[key] = deepClone(value);
            } else {
                targetObj[key] = value;
            }
        }
    }

    return targetObj as T;
}

// 时间戳转换
export function formatTimestamp(timestamp:number): string {
    const date = new Date(timestamp);
    
    // 获取年月日
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份补零
    const day = String(date.getDate()).padStart(2, '0'); // 日期补零
    
    // 获取小时和分钟
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    
    // 组合成目标格式
    return `${year}-${month}-${day} ${hours}:${minutes}`;
}

/**
 * 防抖函数
 * @param fn 需要防抖的函数
 * @param delay 延迟时间（毫秒）
 * @returns 防抖后的函数
 */
export function debounce<T extends (...args: any[]) => any>(
    fn: T,
    delay: number
  ): (...args: Parameters<T>) => void {
    let timer: ReturnType<typeof setTimeout> | null = null;
  
    return function (this: unknown, ...args: Parameters<T>) {
      if (timer) {
        clearTimeout(timer); // 如果已有定时器，清除它
      }
      timer = setTimeout(() => {
        fn.apply(this, args); // 延迟执行
        timer = null; // 执行后清空 timer
      }, delay);
    };
  }

//   透明背景
export const baseOpacityImage = '';